# Copyright (c) 2008-2009 Greg Harfst http://techguyinmidtown.com/
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
"""
File:   EC2PyroController.py
Author: Greg Harfst (greg@techguyinmidtown.com)
Date:   10/12/2008
Notes:  Code to help set up some Pyro clients and servers on Amazon's EC2.

I never really got Pyro up and running completely, but this was my start.
"""

import os
import sys
import time
import EC2Controller as EC2C

class EC2PyroController(EC2C.EC2Controller):
    """
    Subclass of the EC2Controller, containing methods that are particular to
    setting up the Pyro name server and other Pyro clients and servers on EC2.
    """
    def __init__(self, *args, **kwargs):
        """See the EC2Controller constructor for arguments
        """
        EC2C.EC2Controller.__init__(self, *args, **kwargs)
        self.pyroconfigName = "/tmp/Pyro.conf"
        self.pyroWorkingDir = 'pyrowd'

    def startPyroNS(self):
        """Call pyro-ns on the root node."""
        if self.verbose: print "Start pyro-ns on root node"
        self._runOnRoot('pyro-nsd start 2> /dev/null > /dev/null')
        time.sleep(1)
        self._runOnRoot('pyro-esd start 2> /dev/null > /dev/null')
        time.sleep(1)

    def configureFilesOnCluster(self):
        EC2C.EC2Controller.configureFilesOnCluster(self)
        self._createPyroConfigFile()
        hosts = self.getRunningPublicHostnames()
        for host in hosts:
            if self.verbose: print "Configure Pyro on '%s'" % host
            self._run(host, 'mkdir -p %s' % self.pyroWorkingDir)
            self._scp(host, self.pyroconfigName, '%s/Pyro.conf' % self.pyroWorkingDir)

    def _createPyroConfigFile(self):
        """Write a pyro config file that we'll copy over to ec2."""
        privateHostnames = self.getRunningPrivateHostnames()
        if privateHostnames:
            rootHostname = privateHostnames[0]
            if self.verbose: print "Write the pyro config file to '%s'." % self.pyroconfigName
            outfile  = open(self.pyroconfigName, "w")
            print >> outfile, """
PYRO_NS_HOSTNAME=%s
""" % (rootHostname)
            outfile.close()


    ############################################
    ### Code that's particular to my project ###
    ############################################

    def syncDistributedPyroFiles(self):
        filenames = ['./pyrowd/']
        self.syncFilesToCluster(filenames, self.pyroWorkingDir
        

    # 1) start the cluster
    # 2) configure the files on the cluster
    # 3) sync CSV files to the cluster
    # 4) split the dpricing file on every machine
    # 5) configure papyros on the cluster
    # 6) execute the distributed job to build the timeseries

#    def startPyroDaemons(self):
#        self._runOnRoot('pyro-nsd start')
#        time.sleep(1)
#        self._runOnRoot('pyro-esd start')
#        time.sleep(2)

#    def executePapyrosJob(self):
#        self._runOnRoot('cd /usr/local/papyros; time python execute_jobs.py --dispatcher=remote')

#    def runSplitDpricingOnCluster(self):
#        self._runOnAll('rm -rf /usr/local/data/mqaid_split')
#        self._runOnAll('time python /usr/local/papyros/splitCSVOnKey.py /usr/local/data/dpricing_all.csv 1 /usr/local/data/mqaid_split')

#    def configurePapyrosOnCluster(self):
#        # copy over the files
#        self._syncPapyrosFiles()
#        self._startPapyrosDaemons()

#    def _startPapyrosDaemons(self):
#        self._runOnRoot('pyro-nsd start')
#        time.sleep(2)
#        self._runOnRoot('python /usr/local/papyros/start_papyros_daemon.py dispatcher restart')
#        time.sleep(1)
#        self._runOnAll('python /usr/local/papyros/start_papyros_daemon.py workers restart')

#    def _syncPapyrosFiles(self):
#        filenames = ['./papyros/' + fn for fn in ['daemon.py',
#                                                  'execute_jobs.py',
#                                                  'start_papyros_daemon.py',
#                                                  'splitCSVOnKey.py',
#                                                  'tasks.py']]
#        filenames.append('/Users/gharfst/Documents/AdaInvestments/gharfst/ada/risksystem/analytics/src/logic')
#
#        self.syncFilesToCluster(filenames, '/usr/local/papyros')



    def syncCsvdataDir(self):
        filenames = ['../csvdata/' + fn for fn in ['active_universe_gics.csv',
                                                   'dpricing_all.csv',
                                                   'etfs_daily.csv',
                                                   'tradelist_all.csv']]
        filenames.append('../risk.hdf5')
        self.syncFilesToCluster(filenames, '/usr/local/data')

